package com.hr.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hr.entity.Job;
import com.hr.entity.Register;
import com.hr.entity.Unit;
import com.hr.entity.vo.JobQuery;
import com.hr.entity.vo.JobVo;
import com.hr.entity.vo.RegisterVo;
import com.hr.service.JobService;
import com.hr.service.RegisterService;
import com.hr.service.UnitService;
import com.hr.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hr
 * @since 2021-10-15
 */
@CrossOrigin
@RestController
@RequestMapping("/hr/service/job")
public class JobController {

    @Autowired
    JobService jobService;

    @Autowired
    UnitService unitService;

    @PostMapping("jobs/{cur}/{limit}")
    public R get_jobs_page(@PathVariable("cur") long cur,
                     @PathVariable("limit") long limit,
                     @RequestBody JobQuery JobQuery) {

        Page<Job> page = new Page<>(cur, limit);
        QueryWrapper<Job> wrapper = new QueryWrapper<>();

        Integer unitId = JobQuery.getUnitId();
        String name = JobQuery.getName();

        if(!StringUtils.isEmpty(name)) {
            wrapper.like("job_name", name);
        }

        if(unitId != null) {
            wrapper.eq("unit_id", unitId);
        }

        jobService.page(page, wrapper);

        long total = page.getTotal();
        List<Job> records = page.getRecords();
        List<JobVo> jobVos = new ArrayList<>();

        for(Job job: records) {
            JobVo jobVo = new JobVo();
            BeanUtils.copyProperties(job, jobVo);
            Unit unit = unitService.getById(job.getUnitId());
            jobVo.setUnitName(unit.getName());
            jobVos.add(jobVo);
        }
        return R.ok().data("total", total).data("jobs", jobVos);
    }

    @GetMapping("jobs")
    public R get_jobs() {
        List<Job> jobs = jobService.list(null);
        return R.ok().data("jobs", jobs);
    }

    @GetMapping("job")
    public R get_job(String id) {
        Job job = jobService.getById(id);
        Unit unit = unitService.getById(job.getUnitId());

        return R.ok().data("job", job).data("unit", unit);
    }

    @PostMapping("job")
    public R add_job(@RequestBody Job job) {
        boolean save = jobService.save(job);
        return save ? R.ok() : R.error();
    }

    @PutMapping("job")
    public R update_job(@RequestBody Job job) {
        boolean update = jobService.updateById(job);
        return update ? R.ok() : R.error();
    }

    @DeleteMapping("job/{id}")
    public R delete_job(@PathVariable("id") String id) {
        boolean delete = jobService.removeById(id);
        return delete ? R.ok() : R.error();
    }

    @PutMapping("publish/{id}/{status}")
    public R publish_job(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        Job job = new Job();
        job.setId(id);
        job.setStatus(status);
        boolean update = jobService.updateById(job);
        return update ? R.ok() : R.error();
    }
}

